package com.handpay.nocardpay.baselibrary.commbase.view.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import me.yokeyword.fragmentation.SupportActivity;

import com.handpay.nocardpay.baselibrary.NoCardPayApplication;
import com.handpay.nocardpay.baselibrary.R;
import com.handpay.nocardpay.baselibrary.config.NoCardPayConfig;
import com.handpay.nocardpay.baselibrary.config.SecureManager;
import com.handpay.nocardpay.baselibrary.http.HttpHeadFactory;
import com.handpay.nocardpay.baselibrary.logutils.HPLog;
import com.handpay.nocardpay.baselibrary.utils.AccountUtils;
import com.handpay.nocardpay.baselibrary.utils.ClientEngine;
import com.handpay.nocardpay.baselibrary.utils.Heart;
import com.handpay.nocardpay.baselibrary.utils.PreferenceManager;

/**
 * Created by sxshi on 2017/8/8.
 */

public abstract class BaseActivity extends SupportActivity {
    // 再点一次退出程序时间设置
    public static final long WAIT_TIME = 2000L;
    PreferenceManager sharePreference;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        if (savedInstanceState != null) {
            AccountUtils.Account account = (AccountUtils.Account) savedInstanceState.getSerializable("account");
            if (account != null) {
                NoCardPayApplication.getApp().setAccount(account);
                HPLog.d(HPLog.SSX_TAG, "onCreate:" + account.toString());
            }
            NoCardPayConfig.TestEnv env = savedInstanceState.getParcelable("env");
            NoCardPayConfig.ENV = env;
            String sessionId = savedInstanceState.getString("sessionId");
            String keExchange = savedInstanceState.getString("keExchange");
            String onlineHostHeader = savedInstanceState.getString("onlineHostHeader");
            boolean isDoRSA = savedInstanceState.getBoolean("isDoRSA");
            String rsts = savedInstanceState.getString("rsts");
            String seedstr = savedInstanceState.getString("seedstr");
            HttpHeadFactory.sessionID = sessionId;
            HttpHeadFactory.keExchange = keExchange;
            HttpHeadFactory.onlineHostHeader = onlineHostHeader;
            HttpHeadFactory.isDoRSA = isDoRSA;
            HttpHeadFactory.rsts = rsts;
            HttpHeadFactory.seedstr = seedstr;

//            HPLog.d(HPLog.SSX_TAG, "sessionId:" + sessionId);
//            HPLog.d(HPLog.SSX_TAG, "keExchange:" + keExchange);
//            HPLog.d(HPLog.SSX_TAG, "onlineHostHeader:" + onlineHostHeader);
//            HPLog.d(HPLog.SSX_TAG, "isDoRSA:" + isDoRSA);
//            HPLog.d(HPLog.SSX_TAG, "rsts:" + rsts);
//            HPLog.d(HPLog.SSX_TAG, "seedstr:" + seedstr);
//            SecureManager.getInstance().clearKey();
//            SecureManager.getInstance().CheckDesKey();

            if (NoCardPayConfig.isUseHTTPS) {
                ClientEngine.getInstance().init(this, NoCardPayConfig.ENV.HPV, "https://" + NoCardPayConfig.ENV.SERVER.trim() + "/" + NoCardPayConfig.URL_PATH, NoCardPayConfig.ENV.CHAN);
            } else {
                ClientEngine.getInstance().init(this, NoCardPayConfig.ENV.HPV, "http://" + NoCardPayConfig.ENV.SERVER.trim() + ":" + NoCardPayConfig.ENV.PORT + "/" + NoCardPayConfig.URL_PATH, NoCardPayConfig.ENV.CHAN);
            }
        }
        sharePreference = PreferenceManager.getInstance(this);
        Heart.initialize(this);
        initView(savedInstanceState);
        initData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    public abstract void initData();

    public abstract void initView(@Nullable Bundle savedInstanceState);


    public abstract int getLayoutId();


    /***
     * 初始化ToolBar
     *
     * @param toolbar
     * @param leftBackIcon 是否显示左边返回按钮
     * @param rightIcon    是否显示右边设置按钮
     * @param listener     右边按钮监听事件
     */
    protected void initToolbar(Toolbar toolbar, boolean leftBackIcon, boolean rightIcon, View.OnClickListener listener) {
        TextView mTitle = (TextView) toolbar.findViewById(R.id.tv_title);
        ImageView mRightView = (ImageView) toolbar.findViewById(R.id.iv_right);
        toolbar.setTitle("");
        if (null != getActivityTitle()) {
            mTitle.setText(getActivityTitle());
        } else {
            mTitle.setText("");//未设置Fragement title
        }
        if (leftBackIcon) {
            toolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp);
            toolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
        }
        if (rightIcon) {
            mRightView.setVisibility(View.VISIBLE);
            mRightView.setOnClickListener(listener);
        }
    }

    /***
     * @return 返回Fragmnet title
     */
    public String getActivityTitle() {
        return null;
    }

//    @Override
//    public void onRestoreInstanceState(Bundle savedInstanceState) {
//        super.onRestoreInstanceState(savedInstanceState);
//        HPLog.i(HPLog.YL_TAG, "onRestoreInstanceState:");
//        Intent intent = getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackageName());
//        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//        startActivity(intent);
//    }
//
//    @Override
//    protected void onSaveInstanceState(Bundle outState) {
//        super.onSaveInstanceState(outState);
//        HPLog.i(HPLog.YL_TAG, "onSaveInstanceState:");
//
//    }


    @Override
    protected void onSaveInstanceState(Bundle outState) {
        AccountUtils.Account account = NoCardPayApplication.getApp().getAccount();
        if (account != null) {
            outState.putSerializable("account", account);
            HPLog.d(HPLog.SSX_TAG, "onSaveInstanceState:" + account.toString());
        }
        NoCardPayConfig.TestEnv env = NoCardPayConfig.ENV;
        outState.putParcelable("env", env);
        String sessionID = HttpHeadFactory.sessionID;
        String keExchange = HttpHeadFactory.keExchange;
        String onlineHostHeader = HttpHeadFactory.onlineHostHeader;
        String seedstr = HttpHeadFactory.seedstr;
        String rsts = HttpHeadFactory.rsts;
        boolean isDoRSA = HttpHeadFactory.isDoRSA;
        outState.putString("sessionId", sessionID);
        outState.putString("keExchange", keExchange);
        outState.putString("onlineHostHeader", onlineHostHeader);
        outState.putBoolean("isDoRSA", isDoRSA);
        outState.putString("rsts", rsts);
        outState.putString("seedstr", seedstr);
//
//        HPLog.d(HPLog.SSX_TAG, "sessionId:" + sessionID);
//        HPLog.d(HPLog.SSX_TAG, "keExchange:" + keExchange);
//        HPLog.d(HPLog.SSX_TAG, "onlineHostHeader:" + onlineHostHeader);
//        HPLog.d(HPLog.SSX_TAG, "isDoRSA:" + isDoRSA);
//        HPLog.d(HPLog.SSX_TAG, "rsts:" + rsts);
//        HPLog.d(HPLog.SSX_TAG, "seedstr:" + seedstr);
//        HPLog.d(HPLog.SSX_TAG, "onSaveInstanceState:" + env.toString());
        super.onSaveInstanceState(outState);

    }


    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        AccountUtils.Account account = (AccountUtils.Account) savedInstanceState.getSerializable("account");
        if (account != null) {
            NoCardPayApplication.getApp().setAccount(account);
            HPLog.d(HPLog.SSX_TAG, "onRestoreInstanceState:" + account.toString());
        }
        NoCardPayConfig.TestEnv env = savedInstanceState.getParcelable("env");
        NoCardPayConfig.ENV = env;

        String sessionId = savedInstanceState.getString("sessionId");
        String keExchange = savedInstanceState.getString("keExchange");
        String onlineHostHeader = savedInstanceState.getString("onlineHostHeader");
        boolean isDoRSA = savedInstanceState.getBoolean("isDoRSA");
        String rsts = savedInstanceState.getString("rsts");
        String seedstr = savedInstanceState.getString("seedstr");

//        HPLog.d(HPLog.SSX_TAG, "sessionId:" + sessionId);
//        HPLog.d(HPLog.SSX_TAG, "keExchange:" + keExchange);
//        HPLog.d(HPLog.SSX_TAG, "onlineHostHeader:" + onlineHostHeader);
//        HPLog.d(HPLog.SSX_TAG, "isDoRSA:" + isDoRSA);
//        HPLog.d(HPLog.SSX_TAG, "rsts:" + rsts);
//        HPLog.d(HPLog.SSX_TAG, "seedstr:" + seedstr);
        HttpHeadFactory.sessionID = sessionId;
        HttpHeadFactory.keExchange = keExchange;
        HttpHeadFactory.onlineHostHeader = onlineHostHeader;
        HttpHeadFactory.isDoRSA = isDoRSA;
        HttpHeadFactory.rsts = rsts;
        HttpHeadFactory.seedstr = seedstr;

        HPLog.d(HPLog.SSX_TAG, "onRestoreInstanceState:" + env.toString());
//        SecureManager.getInstance().clearKey();
//        SecureManager.getInstance().CheckDesKey();

        if (NoCardPayConfig.isUseHTTPS) {
            ClientEngine.getInstance().init(this, NoCardPayConfig.ENV.HPV, "https://" + NoCardPayConfig.ENV.SERVER.trim() + "/" + NoCardPayConfig.URL_PATH, NoCardPayConfig.ENV.CHAN);
        } else {
            ClientEngine.getInstance().init(this, NoCardPayConfig.ENV.HPV, "http://" + NoCardPayConfig.ENV.SERVER.trim() + ":" + NoCardPayConfig.ENV.PORT + "/" + NoCardPayConfig.URL_PATH, NoCardPayConfig.ENV.CHAN);
        }
        super.onRestoreInstanceState(savedInstanceState);
    }

}
